package ex03.pyrmont.connector.http;

import com.sky.JULLog.MyLogger;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Logger;
/**
 * http://localhost:8080/servlet/PrimitiveServlet
 * */
public class HttpConnectorV3 implements Runnable {

    private static Logger  log  = MyLogger.getLogger(HttpConnectorV3.class);


    boolean stopped =false;
    private String scheme = "http";

    public String getScheme() {
        return scheme;
    }

    public void run() {
        ServerSocket serverSocket = null;
        int port = 8080;
        try {
            serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
        log.info("服务器已启动,  开始进行监听.........");
        while (!stopped) {
            // Accept the next incoming connection from the server socket
            Socket socket = null;
            try {
                socket = serverSocket.accept();
            } catch (Exception e) {
                continue;
            }
            // Hand this socket off to an HttpProcessorV3
            HttpProcessorV3 processor = new HttpProcessorV3(this);
            stopped =  processor.process(socket);
        }
        log.warning("服务器停止服务............");
    }

    public void start() {
        Thread thread = new Thread(this);
        thread.start();
    }
}
